<!--
  * BOM表
  *
  * @Author:    AKITA
  * @Date:      2025-08-15 10:54:43
  * @Copyright  AKITA
-->
<template>
  <a-drawer
      :title="form.id ? '编辑' : '添加'"
      width="40%"
      :open="visibleFlag"
      @close="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >

      <a-form-item label="用户编码"  name="userId">
        <a-input style="width: 100%" v-model:value="form.userId" placeholder="用户编码" />
      </a-form-item>
      <a-form-item label="用户名称"  name="userName">
        <a-input style="width: 100%" v-model:value="form.userName" placeholder="用户名称" />
      </a-form-item>
      <a-form-item label="认证日期时间"  name="authDateTime">
        <a-date-picker style="width: 100%" valueFormat="YYYY-MM-DD HH:mm:ss" showTime  v-model:value="form.authDateTime" placeholder="认证日期时间" />
      </a-form-item>
      <a-form-item label="方向"  name="direction">
        <a-select v-model:value="form.direction" placeholder="方向">
          <a-select-option key="1">上班</a-select-option>
          <a-select-option key="0">下班</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="设备编码"  name="macId">
        <a-input style="width: 100%" v-model:value="form.macId" placeholder="设备编码" />
      </a-form-item>
      <a-form-item label="设备名称"  name="macName">
        <a-input style="width: 100%" v-model:value="form.macName" placeholder="设备名称" />
      </a-form-item>
      <a-form-item label="卡号"  name="cardId">
        <a-input style="width: 100%" v-model:value="form.cardId" placeholder="卡号" />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue';
import _ from 'lodash';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { tmsordApi } from '/@/api/business/erp/tmsord-api';
import { smartSentry } from '/@/lib/smart-sentry';

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
  // if (form.status && form.status.length > 0) {
  //   form.status = form.status.map((e) => e.valueCode);
  // }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  id: undefined, //主键
  userId: undefined, //用户编码
  userName: undefined, //用户名称
  authDateTime: undefined, //认证日期时间
  authDate: undefined, //认证日期
  zuthTime: undefined, //认证时间
  direction: undefined, //方向
  macId: undefined, //设备编码
  macName: undefined, //设备名称
  cardId: undefined, //卡号
};

let form = reactive({ ...formDefault });

const rules = {
  userId: [{ required: true, message: '用户编码不能为空' }],
  userName:[{ required: true, message: '用户名称不能为空' }],
  authDateTime:[{ required: true, message: '认证时间不能为空' }],
  direction:[{ required: true, message: '方向不能为空' }],
  macId:[{ required: true, message: '设备编码不能为空' }],
  macName:[{ required: true, message: '设备名称不能为空' }],
};

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    if (form.id) {
      await tmsordApi.update(form);
    } else {
      await tmsordApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});
</script>
